const {
  http
} = require("../../utils/apiutils");

// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    ptype: "",
    id: "",
    canIUseGetUserProfile: false
  },
  getPhoneNumber: function (e) {
    console.log("----------", e);
    if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
      wx.showToast({
        title: '取消授权',
        icon: "none",
      })
    } else if (e.detail.errMsg == "getPhoneNumber:ok") {
      var encryptedData = e.detail.encryptedData;
      var iv = e.detail.iv;
      console.log(getApp().globalData)
      var data = {
        "openid": getApp().globalData.openid,
        "unionid": getApp().globalData.unionid,
        "encryptedData": encryptedData,
        "iv": iv,
        inviteCode: getApp().globalData.inviteCode,
        event: getApp().globalData.isActivity ? 1 : '',
        userFrom:4
      }
      if (getApp().globalData.isActivity) {
        data.id = getApp().globalData.rId
      }
      var that = this;
      wx.showLoading({
        title: '加载中...',
      })
      http.wechatBindPhone(data,
        function (res) {
          wx.hideLoading()
          console.log("------用户信息--", res);
          if (res.code == 0) {
            getApp().globalData.token = res.token
            getApp().globalData.isLogin = true;
            that.getuser();
            wx.showToast({
              title: "绑定手机号成功",
              icon: "none",
              success() {
                if (getApp().globalData.route) {
                  let url = ""
                  switch (getApp().globalData.route) {
                    case 'searchgoods':
                      url = '/pages/home/pages/searchgoods/searchcar'
                      break;
                    case 'searchcar':
                      url = '/pages/home/pages/searchcar/searchcar'
                      break;
                    case 'searchGDtl':
                      url = '/pages/home/pages/searchGDtl/index?'+getApp().globalData.routeParams
                      break;
                    case 'activity':
                      url = '/pages/home/pages/activity/activity?'+getApp().globalData.routeParams
                      break;
                    case 'information':
                      url = '/pages/tabBar/information/index'
                      break;
                    case 'list-dtl':
                      url = '/pages/information/pages/list-dtl/index?'+getApp().globalData.routeParams
                      break;
                    case 'work':
                      url = '/pages/home/pages/work/work?'+getApp().globalData.routeParams
                      break;
                    case 'renwuwork':
                      url = '/pages/home/pages/renwuwork/work?'+getApp().globalData.routeParams
                      break;
                    case 'jobvideo':
                      url = '/pages/clock/pages/jobvideo/index?'+getApp().globalData.routeParams
                      break;
                  }
                  wx.reLaunch({
                    url: '/pages/tabBar/home/index',
                    success() {
                      wx.reLaunch({
                        url: url,
                        success() {
                          getApp().globalData.route = ""
                        }
                      })
                    }
                  })
                } else if (that.data.ptype == "home") {
                  wx.reLaunch({
                    url: '/pages/tabBar/home/index',
                    success() {}
                  })
                } else if (that.data.ptype == "nearby") {
                  wx.reLaunch({
                    url: '/pages/tabBar/home/index',
                    success() {
                      wx.reLaunch({
                        url: '/pages/tabBar/nearby/index',
                        success() {}
                      })
                    }
                  })
                } else if (that.data.ptype == "me") {
                  wx.reLaunch({
                    url: '/pages/tabBar/home/index',
                    success() {
                      wx.reLaunch({
                        url: '/pages/tabBar/me/index',
                        success() {}
                      })
                    }
                  })
                } else if (that.data.ptype == "work") {
                  wx.reLaunch({
                    url: '/pages/tabBar/home/index',
                    success() {
                      wx.reLaunch({
                        url: '/pages/home/pages/work/work?id=' + that.data.id,
                        success() {}
                      })
                    }
                  })
                } else {
                  wx.reLaunch({
                    url: '/pages/tabBar/home/index',
                  })
                }
              }
            })

          } else if (res.code == 1) {
            wx.showToast({
              title: res.message,
              icon: "none"
            })
            that.setData({
              type: 1
            })
          }
        },
        function (err) {
          wx.hideLoading()
        })
      console.log("----------", data);
    }

  },
  onclickBack: function () {

    wx.navigateBack({
      delta: 1,
    })
  },
  getUserProfile: function (e) {
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (e) => {
        console.log("----------", e);
        if (e.errMsg == "getUserProfile:fail auth deny") {
          wx.showToast({
            title: '取消授权',
            icon: "none",
          })
        } else if (e.errMsg == "getUserProfile:ok") {
          var encryptedData = e.encryptedData;
          var iv = e.iv;
          var data = {
            "openid": getApp().globalData.openid,
            "unionid": getApp().globalData.unionid,
            "encryptedData": encryptedData,
            "iv": iv,
            inviteCode: getApp().globalData.inviteCode,
            event: getApp().globalData.isActivity ? 1 : '',
            userFrom:4
          }
          var that = this;
          if (getApp().globalData.isActivity) {
            data.id = getApp().globalData.rId
          }
          wx.showLoading({
            title: '加载中...',
          })
          http.wechatBindInfo(data,
            function (res) {
              wx.hideLoading()
              console.log("------用户信息--", res);
              if (res.code == 0) {
                that.wclogin();
              } else if (res.code == 2) {
                wx.showToast({
                  title: '绑定微信成功，请绑定手机号',
                  icon: "none"
                })
                that.setData({
                  type: 2
                })
              }
            },
            function (err) {
              wx.hideLoading()
            })
          console.log("----------", data);
        }
      }
    })

  },
  getUserInfo: function (e) {
    console.log("----------", e);
    if (e.detail.errMsg == "getUserInfo:fail auth deny") {
      wx.showToast({
        title: '取消授权',
        icon: "none",
      })
    } else if (e.detail.errMsg == "getUserInfo:ok") {
      var encryptedData = e.detail.encryptedData;
      var iv = e.detail.iv;
      var data = {
        "openid": getApp().globalData.openid,
        "unionid": getApp().globalData.unionid,
        "encryptedData": encryptedData,
        "iv": iv,
        userFrom:4,
        inviteCode: getApp().globalData.inviteCode,
        event: getApp().globalData.isActivity ? 1 : '',
      }
      if (getApp().globalData.isActivity) {
        data.id = getApp().globalData.rId
      }
      var that = this;
      wx.showLoading({
        title: '加载中...',
      })
      http.wechatBindInfo(data,
        function (res) {
          wx.hideLoading()
          console.log("------用户信息--", res);
          if (res.code == 0) {
            that.wclogin();
          } else if (res.code == 2) {
            wx.showToast({
              title: '绑定微信成功，请绑定手机号',
              icon: "none"
            })
            that.setData({
              type: 2
            })
          }
        },
        function (err) {
          wx.hideLoading()
        })
      console.log("----------", data);
    }
  },

  wclogin: function () {
    var that = this;
    wx.login({ //调用获取用户openId
      success: function (res) {
        that.wxlogin(res.code)
      }
    })
  },

  wxlogin: function (code) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.weChatLogin({
        code: code
      },
      function (res) {
        wx.hideLoading()
        console.log("------用户信息--", res);
        if (res.code == 0) {
          getApp().globalData.isLogin = true;
          getApp().globalData.token = res.token;
          getApp().globalData.unionid = res.unionid;
          that.getuser();
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },

  //获取用户信息
  getuser: function () {

    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.userInfo({},
      function (res) {
        wx.hideLoading()
        console.log("------用户信息--", res);
        if (res.code == 0) {
          getApp().globalData.userInfo = res.data;
          getApp().globalData.isLogin = true;
          if (that.data.ptype == "home") {
            wx.reLaunch({
              url: '/pages/tabBar/home/index',
            })
          } else if (that.data.ptype == "nearby") {
            wx.reLaunch({
              url: '/pages/tabBar/home/index',
              success() {
                wx.reLaunch({
                  url: '/pages/tabBar/nearby/index',
                })
              }
            })
          } else if (that.data.ptype == "me") {
            wx.reLaunch({
              url: '/pages/tabBar/home/index',
              success() {
                wx.reLaunch({
                  url: '/pages/tabBar/me/index',
                })
              }
            })
          } else if (that.data.ptype == "work") {
            wx.reLaunch({
              url: '/pages/tabBar/home/index',
              success() {
                wx.reLaunch({
                  url: '/pages/home/pages/work/work?id=' + that.data.id,
                  success() {}
                })
              }
            })
          } else if (getApp().globalData.route) {
            let url = ""
            switch (getApp().globalData.route) {
              case 'searchgoods':
                url = '/pages/home/pages/searchgoods/searchcar'
                break;
              case 'searchcar':
                url = '/pages/home/pages/searchcar/searchcar'
                break;
              case 'searchGDtl':
                url = '/pages/home/pages/searchGDtl/index?'+getApp().globalData.routeParams
                break;
              case 'activity':
                url = '/pages/home/pages/activity/activity?'+getApp().globalData.routeParams
                break;
              case 'information':
                url = '/pages/tabBar/information/index'
                break;
              case 'list-dtl':
                url = '/pages/information/pages/list-dtl/index?'+getApp().globalData.routeParams
                break;
              case 'work':
                url = '/pages/home/pages/work/work?'+getApp().globalData.routeParams
                break;
              case 'renwuwork':
                url = '/pages/home/pages/renwuwork/work?'+getApp().globalData.routeParams
                break;
              case 'jobvideo':
                url = '/pages/clock/pages/jobvideo/index?'+getApp().globalData.routeParams
                break;
                
            }
            wx.reLaunch({
              url: '/pages/tabBar/home/index',
              success() {
                wx.reLaunch({
                  url: url,
                  success() {
                    getApp().globalData.route = ""
                  }
                })
              }
            })
          } else {
            wx.reLaunch({
              url: '/pages/tabBar/home/index',
            })
          }
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.ptype) {
      this.setData({
        ptype: options.ptype,
      })
      if (options.ptype == "work") {
        this.setData({
          id: options.id
        })
      }
    }
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    this.setData({

      type: options.type,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: getApp().globalData.userInfo.inviteCode ? '/pages/index/index?code=' + getApp().globalData.userInfo.inviteCode : '/pages/index/index', // 
      imageUrl: '',
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {}
      }
    }
    console.log("------------", shareObj);
    // 返回shareObj
    return shareObj;
  }
})